<?php
include('../../includes/classes/core.php');
$REQ_data   = $_REQUEST[data];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $REQ_data[done]%3;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "შემოსული  ზარები ზარის ტიპების მიხედვით ";
$text[1] 	= "'$type'- შემოსული ზარები  კატეგორიების მიხედვით";
$text[2] 	= "'$category'- შემოსული ზარები  ქვე-კატეგორიების მიხედვით";

if ($REQ_data[sub][0]=="პრობლემა")  $c=1;
elseif ($REQ_data[sub][0]=="ინფორმაცია") $c=2;
elseif ($REQ_data[sub][0]=="სხვა") $c=3;

$start = $db->convertDateTime($REQ_data[start]);
$end = $db->convertDateTime($REQ_data[end]);

//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("		SELECT category.`name`,
											COUNT(*),
											CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
												FROM 	incomming_call
												JOIN 	category ON category.id=incomming_call.call_subcategory_id
												WHERE (incomming_call.call_type_id=$c)  and `incomming_call`.`date` >= '$start' and  DATE_FORMAT(`incomming_call`.`date`,'%d/%m/%y %H:%i:%s') <= '$REQ_data[end]'
												)*100,2),'%')
									FROM 	incomming_call
									JOIN 	category ON category.id=incomming_call.call_subcategory_id
									WHERE (incomming_call.call_type_id=$c) and `incomming_call`.`date` >= '$start' and  DATE_FORMAT(`incomming_call`.`date`,'%d/%m/%y %H:%i:%s') <= '$REQ_data[end]'
									GROUP BY category.`name`");
		$text[0]=$text[1];
	break;
	case 2:
			$result = mysql_query("SELECT 	category.`name` as c_name,
										COUNT(*),
										CONCAT(ROUND(
										COUNT(*)/(SELECT COUNT(*)
											FROM incomming_call
											JOIN category AS inf1 ON inf1.`name`='".$REQ_data[sub][$done-1]."'
											JOIN category ON category.id=incomming_call.call_category_id AND category.parent_id=inf1.id
											WHERE (incomming_call.call_type_id=$c) and `incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end'
											)*100,2),'%')
								FROM 	incomming_call
								JOIN 	category AS inf1 ON inf1.`name`='".$REQ_data[sub][$done-1]."'
								JOIN 	category ON category.id=incomming_call.call_category_id AND category.parent_id=inf1.id
								WHERE (incomming_call.call_type_id=$c) and `incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end'
								GROUP BY c_name");
		$text[0]=$text[2];
	break;
	default:
		$result =  mysql_query("SELECT IF(incomming_call.call_type_id=1,'პრობლემა',IF(incomming_call.call_type_id=2,'ინფორმაცია','სხვა')) as type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
											WHERE `incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end')*100,2),'%')
								FROM 	incomming_call
								WHERE 	`incomming_call`.`date` >= '$start' and  `incomming_call`.`date` <= '$end'
								GROUP BY 	type");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":	   
	  //  print_r($data);
	 //   echo "fff";
	//    return 0;
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
	case 'get_in_page':   

		mysql_query("SET @i = 0;");
$rResult = mysql_query("SELECT  		inc.id,    
                        						inc.id, 
                        						DATE_FORMAT(inc.`date`,'%d/%m/%y %H:%i:%s') AS `date`,
                        						inc.`pin`,
                        						inc.`pid`,
                        						inc.phone,
                        						category.`name`,
                        						`crystal_users`.`name` ,
                        						`crystal_users1`.`name` ,
                        						if(ISNULL(task.incomming_call_id), 'დასრულებული', `status`.`call_status`)AS `status`,	
                      							TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration - 1), '%i:%s') AS `duration`,
												CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>')AS `file_name`
								FROM 				incomming_call AS inc
                                LEFT JOIN 	category  ON inc.call_category_id=category.id
                                LEFT JOIN 	task ON task.incomming_call_id=inc.id								
                                LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
                                LEFT JOIN 	crystal_users   AS `crystal_users1`	ON inc.user_id=crystal_users1.id
                                LEFT JOIN 	`status` ON task.`status`=`status`.id
								LEFT JOIN asterisk_incomming ON inc.uniqueid=asterisk_incomming.uniqueid
								WHERE `inc`.`date` >= '$start' AND `inc`.`date` <= '$end'
					 						AND 
	  											category.`name`='$REQ_data[rID]' and (inc.call_type_id=$c)
	  			
								GROUP BY inc.id");
	  
$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[] = $aRow[$i];
				$a=$aRow;
				
				{
				
				}
			}
			$data['aaData'][] = $row;
		}
		echo json_encode($data); return 0;
		break;
	default :
		echo "Action Is Null!";
		break;

}



?>